package com.niiwoo.civet.user.service.local;

import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.niiwoo.civet.user.dao.entity.UserCreditCardAuthInfo;
import com.niiwoo.civet.user.dao.mapper.UserCreditCardAuthInfoMapperExt;
import com.niiwoo.civet.user.dto.response.UserRealNameInfoDTO;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.tiancheng.enums.FunctionCodeEnum;
import com.niiwoo.tripod.tiancheng.request.auth.YXCreditDataQueryRequest;
import com.niiwoo.tripod.tiancheng.request.df.CardniuCreditCardAnalysisRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: wuke
 * @Description:
 * @Date: Created in 14:56 2018/3/7
 */
@Slf4j
@Service
public class UserYXCreditCardService {
    @Autowired
    UserDataService UserDataService;
    @Autowired
    UserCreditCardAuthInfoMapperExt userCreditCardAuthInfoMapper;
    @Autowired
    PrivacyMasks privacyMasks;

    @Transactional
    public YXCreditDataQueryRequest getYXCreditDataQueryRequest(String userId){

        //指定读主库
        if (!HintManagerHolder.isMasterRouteOnly()) {
            HintManagerHolder.clear();
            HintManager hintManager = HintManager.getInstance();
            hintManager.setMasterRouteOnly();
        }

        UserCreditCardAuthInfo userCreditCardAuthInfo = ((UserYXCreditCardService)AopContext.currentProxy()).queryUserCreditCardAuthInfo(userId);
        if(userCreditCardAuthInfo==null){
            throw new BizException("USR19096");
        }

        UserRealNameInfoDTO userRealNameInfo = UserDataService.getUserRealNameInfo(userId);

        YXCreditDataQueryRequest request = new YXCreditDataQueryRequest();
        request.setHouseName(userRealNameInfo.getRealName());
        request.setIdCardNo(privacyMasks.decryptPrivacy(userRealNameInfo.getIdCard()));
        request.setPhone(privacyMasks.decryptPrivacy(userRealNameInfo.getMobileNo()));
        request.setLoginName(userCreditCardAuthInfo.getLoginName());
        request.setEntryId(userCreditCardAuthInfo.getEntryId());

        return request;
    }

    @Transactional
    public UserCreditCardAuthInfo queryUserCreditCardAuthInfo(String userId){
        return userCreditCardAuthInfoMapper.selectByPrimaryKey(userId);
    }

    public CardniuCreditCardAnalysisRequest getCreditCardAnalysisRequest(String userId){
        UserCreditCardAuthInfo userCreditCardAuthInfo = userCreditCardAuthInfoMapper.selectByPrimaryKey(userId);
        UserRealNameInfoDTO userRealNameInfo = UserDataService.getUserRealNameInfo(userId);

        CardniuCreditCardAnalysisRequest request = new CardniuCreditCardAnalysisRequest();
        request.setUserId(userId);
        request.setRealName(userRealNameInfo.getRealName());
        request.setIdentityCard(privacyMasks.decryptPrivacy(userRealNameInfo.getIdCard()));
        request.setMobilePhone(privacyMasks.decryptPrivacy(userRealNameInfo.getMobileNo()));
        request.setLoginName(userCreditCardAuthInfo.getLoginName());
        request.setEntryId(userCreditCardAuthInfo.getEntryId());
        request.setFunctionCode(FunctionCodeEnum.CARDNIU_CREDIT_CARD_ANALYSIS.getBodyFunctionCode());

        return request;
    }
}
